<?php

/**
 * 
 */
use Doctrine\ORM\Query\Expr\Select;
class Safety_Form_Privilege extends Zend_Validate_Abstract {
	/**
	 * @var Zend_Log
	 */	
	protected $_logger = false;
	const MODULE	 = 'moduleFilter';
	const CONTROLLER	 = 'controllerFilter';
	const ACTION	 = 'actionFilter';
	const INVALID_DATA = 'invalidData';
	
    /**
     * @var array
     */
	protected $_messageTemplates = array(
		self::MODULE => "",
		self::CONTROLLER => "",
		self::ACTION => "",
		self::INVALID_DATA => "Invalid Data"
	);
	
	public function __construct() {
		$this->_logger = Zend_Registry::get('Zend_Log');
	}
	
	/** 
	 * Valida los campos del formulario
	 * @return boolean
	 * @author David Quispert Lazarte.
	 */
	
	public function isValid($options) {
		if (!is_array($options)) {//
            $this->_error(self::INVALID_DATA);
            return false;
        }
        // filtering data
		$formData = $this->filterFields($options);
				
		// validation data
		$validateStringLength = new Zend_Validate_StringLength(1,100);
		if ( !$validateStringLength->isValid($formData[self::MODULE]) ) {		
			$this->_messageTemplates[self::MODULE] = "The module, " . implode(".",$validateStringLength->getMessages());			
			$this->_error(self::MODULE);			
		}
		
		$validateStringLength = new Zend_Validate_StringLength(1,100);
		if ( !$validateStringLength->isValid($formData[self::CONTROLLER]) ) {
			$this->_messageTemplates[self::CONTROLLER] = "The controller, " . implode(".",$validateStringLength->getMessages());
			$this->_error(self::CONTROLLER);
		}
		
		$validateStringLength = new Zend_Validate_StringLength(1,100);
		if ( !$validateStringLength->isValid($formData[self::ACTION]) ) {
			$this->_messageTemplates[self::ACTION] = "The action, " . implode(".",$validateStringLength->getMessages());
			$this->_error(self::ACTION);
		}
		
		// if there messages then false
		if (count($this->_messages)) {
            return false;
        } else {
            return true;
        }
	}
	
	/**
	 * Filtering data
	 * @param array $options
	 * @return array returns the filtering data	 
	 */
	public function filterFields($options) {
		// filters available
		$filterChain = new Zend_Filter();
		$filterChain->addFilter(new Zend_Filter_StripTags());
		
		$options[self::MODULE] = $filterChain->filter($options[self::MODULE]);
		$options[self::CONTROLLER] = $filterChain->filter($options[self::CONTROLLER]);
		$options[self::ACTION] = $filterChain->filter($options[self::ACTION]);
		   
		return $options;
	} 
	
}